package assignment2;

/*import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;*/

/*
 * For question #3:
 * If both consumers are running and subscribed,
 * then they both receives the messages.
 */
/*
public class Consumer {

	public static void main(String[] args) {
		
		// Avoid blocking
		new Thread(new Runnable(){
			public void run() {
				Consumer c = new Consumer();	
				while (true) {	
					System.out.println(c.receive());
				}
			}			
		}).start();
		
		// We can do other things here...
	}
	
	public String receive()	{
		try	{
			Context ctx = new InitialContext();

			// Lookup the TopicConnectionFactory
			TopicConnectionFactory tfc = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
			
			// Lookup the Topic
			Topic t = (Topic) ctx.lookup("Messages");
			
			// Create a new Topic Connection
			TopicConnection tc = tfc.createTopicConnection();
			
			// Start a new Topic Session
			TopicSession ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			
			// Create the subscriber, we only want to receive messages from 'Alice'
			TopicSubscriber tsc = ts.createSubscriber(t, "sender = 'Alice'", false);
			
			// Start the topic connection
			tc.start();
			
			// Receive the message
			TextMessage m = (TextMessage) tsc.receive();
			
			// Return the message text
			return (m != null ? m.getText() : null);
		} catch (Exception e) {		
			System.out.println("Exception:" + e.getMessage());
			e.printStackTrace();	
			return null;
		}		
	}	

}*/
